/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package terminal;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import kret.ui.ActionManager;
import kret.ui.interfaces.ICommandListener;
import terminal.model.SerialModel;
import terminal.ui.contexts.DialogAboutContext;
import terminal.ui.contexts.DialogPortOpenContext;
import terminal.ui.contexts.FrameMainContext;
import terminal.ui.forms.*;

/**
 *
 * @author P. Podwapinski
 */
public class UiBuilder implements Runnable {

    public UiBuilder(final String uiLookAndFeelName, SerialModel serialModel) 
    {
        m_lafName = uiLookAndFeelName;
        m_serialModel = serialModel;
    }
    
    @Override
    public void run() 
    {
        createUi();
        showUi();
    }
    
    
    private void createUi()
    {     
        m_frameMain = new FrameMain();
        m_dialogPortOpen = new DialogPortOpen( m_frameMain, true );
        m_dialogAbout = new DialogAbout( m_frameMain, true );

        m_actionManager = new ActionManager();

        buildActions();

        m_frameMainContext = new FrameMainContext(m_actionManager, m_frameMain, m_serialModel);
        m_dialogPortOpenContext = new DialogPortOpenContext(m_actionManager, m_dialogPortOpen, m_serialModel);
        m_dialogAboutContext = new DialogAboutContext(m_actionManager, m_dialogAbout);

        m_frameMainContext.addChildContext("DLG_PORTOPEN", m_dialogPortOpenContext);
        m_frameMainContext.addChildContext("DLG_ABOUT", m_dialogAboutContext);        
    }
    

    private void showUi() 
    {
        if ( null != m_frameMainContext )
        {
            m_frameMainContext.showFrame();
        }
    }

    private void buildActions() 
    {
        if ( null != m_actionManager ) 
        {
            m_actionManager.addAction("About...", ICommandListener.COMMAND_DIALOG_ABOUT_SHOW, "Shows The About Window");
            m_actionManager.addAction("Close", ICommandListener.COMMAND_DIALOG_ABOUT_CLOSE, "Closes The About Window");
            m_actionManager.addAction("Exit", ICommandListener.COMMAND_APP_EXIT, "Exit jTerminal Application");
            m_actionManager.addAction("Port Open...", ICommandListener.COMMAND_DIALOG_OPENPORT_SHOW, "Opens Port Selection Dialog");
            m_actionManager.addAction("Port Close", ICommandListener.COMMAND_PORT_CLOSE, "Closes The Currently Opened Port");
            m_actionManager.addAction("Open", ICommandListener.COMMAND_PORT_OPEN, "Open COM Port");
            m_actionManager.addAction("Cancel", ICommandListener.COMMAND_DIALOG_OPENPORT_CLOSE, "Cancel");

            m_actionManager.addAction("Terminal...", ICommandListener.COMMAND_MDI_OPEN_TERMINAL, "Show Terminal Window");
            m_actionManager.addAction("New Filter...", ICommandListener.COMMAND_MDI_OPEN_FILTER, "Show New Filter Window");
            m_actionManager.addAction("Close All", ICommandListener.COMMAND_MDI_WINDOWS_CLOSE, "Close All Opened Windows");
        }
    }

    private void applyLookAndFeel(final String lookAndFeelName) {
        UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
        String lookAndFeelClassName = null;

        for (int i = 0; i < looks.length; i++) {
            if (lookAndFeelName.equals(looks[i].getName())) {
                lookAndFeelClassName = looks[i].getClassName();
                break;
            }
        }

        try {
            UIManager.setLookAndFeel(lookAndFeelClassName);
            SwingUtilities.updateComponentTreeUI(m_frameMain);
            SwingUtilities.updateComponentTreeUI(m_dialogAbout);
            SwingUtilities.updateComponentTreeUI(m_dialogPortOpen);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(m_frameMain, "Can't change look and feel:"
                    + lookAndFeelClassName, "Invalid PLAF",
                    JOptionPane.ERROR_MESSAGE);
        }
    }
    
    private String m_lafName;
    private SerialModel m_serialModel;
    
    private FrameMain m_frameMain;
    private FrameMainContext m_frameMainContext;
    private DialogPortOpen m_dialogPortOpen;
    private DialogPortOpenContext m_dialogPortOpenContext;
    private DialogAbout m_dialogAbout;
    private DialogAboutContext m_dialogAboutContext;
    
    private ActionManager m_actionManager;
}
